package test;

import static org.junit.Assert.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import vc.bean.SearchHits;
import vc.dubbo.result.DubboResult;
import vc.search.service.ResourceService;
import vc.search.service.SearchService;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vcg.community.dao.ResourceMapper;
import com.vcg.community.model.*;
import com.vcg.community.service.FavoriteService;
import com.vcg.community.service.MasterService;
import com.vcg.community.service.ResourceGetService;
import com.vcg.community.service.UploadService;
import com.vcg.community.service.UserGetService;
import com.vcg.community.service.VoteService;


public class dubboConsumer {

	@Autowired
	FavoriteService fService;
	@Autowired
	ResourceMapper rm;
	@Autowired
	ResourceService resourceService;
	@Test
	public void test() {
		//testServiceGet();
		//testUpload();
		//testFavorite();
		//testFavoriteWithAnnoation();
		//testResource();
		//testSearch();
		//testUserGet();
		//testGetVoteAndFavoriteList();
		//testSearch2();
		testMasterService();
	}
	private void testMasterService(){
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        MasterService demoService = (MasterService)context.getBean("masterService"); // 获取远程服务代理
		DubboResult<List<String>> dbResult=demoService.getMasterData("category");
		assertEquals(null,dbResult.getResult().get(0));
	
	}
	private void testServiceGet(){
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        ResourceGetService demoService = (ResourceGetService)context.getBean("doGetResource"); // 获取远程服务代理
//        DubboResult<Resource> drResult= demoService.doGetDetail("60656062389b4664bc68126b4b96b206"); // 执行远程方法
//		assertEquals("japan",drResult.getResult().getTitle());
		
	}
	private void testGetVoteAndFavoriteList(){
		
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        FavoriteService demoService = (FavoriteService)context.getBean("favoriteService"); // 获取远程服务代理
        DubboResult<List<Resource>> drResult= demoService.getFavoritedResources(1); // 执行远程方法
        //assertEquals(0,drResult.getCode());
        assertEquals(10,drResult.getResult().size());
        VoteService demoService2 = (VoteService)context.getBean("voteService"); // 获取远程服务代理
        DubboResult<List<Resource>> drResult2= demoService2.getVotedResources(1); // 执行远程方法
        //assertEquals(0,drResult.getCode());
        assertEquals(10,drResult2.getResult().size());
 
	}


	private void testUserGet(){
		
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        UserGetService demoService = (UserGetService)context.getBean("doGetUser"); // 获取远程服务代理
        DubboResult<User> drResult= demoService.getDetail(100); // 执行远程方法
        //assertEquals(0,drResult.getCode());
        assertEquals(null,drResult.getResult());
        DubboResult<List<User>> drResult1= demoService.getFriends(2); // 执行远程方法
        //assertEquals(0,drResult.getCode());
        assertEquals(1,drResult1.getResult().size());
        DubboResult<List<User>> drResult2= demoService.getFollowers(2); // 执行远程方法
        //assertEquals(0,drResult.getCode());
        assertEquals(2,drResult2.getResult().size());
 
	}

	
	private void testSearch(){
//		Resource r=new Resource();
//		r.setId(1);
//		r.setResourceId("abcduuidabcduuid-inside-provider");
//		r.setTitle("HaianTestInSocialInProvider");

		Resource r=rm.selectByResourceID("b151c858bbb144fabb0f64628b51bd3f");
		assertEquals("japan",r.getTitle());

		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        ResourceService demoService = (ResourceService)context.getBean("resourceService"); // 获取远程服务代理
		String jsonRSString = JSON.toJSONString(r);
        JSONObject jsonRSObject = JSON.parseObject(jsonRSString);  
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jsonRSObject.remove("createddate");
        jsonRSObject.remove("id");
        jsonRSObject.remove("resourceId");
        jsonRSObject.put("created_date",format1.format(new Date()) );
        jsonRSObject.put("user",jsonRSObject.get("uploadName") );
        DubboResult<String> strResult=demoService.index(r.getResourceId(), jsonRSObject );
        assertEquals(200,strResult.getCode());
	}

	private void testSearch2(){
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        ResourceService demoService = (ResourceService)context.getBean("resourceService"); // 获取远程服务代理
        DubboResult<SearchHits> strResult=demoService.search("Select * from index.resource ");
        assertEquals(strResult,null);
	}

	private void testResource(){
		Resource r=new Resource();
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        ResourceGetService demoService = (ResourceGetService)context.getBean("doGetResource"); // 获取远程服务代理
  //      DubboResult<Resource> lResult=demoService.doGetDetail(new String("abcd"));; // 执行远程方法
 
 //       assertEquals(lResult,null); // 显示调用结果

	}

	
	private void testFavorite(){
		Resource r=new Resource();
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
 //       FavoriteService demoService = (FavoriteService)context.getBean("doFavorite"); // 获取远程服务代理
 //       demoService.doFavorite(r);; // 执行远程方法
 
        System.out.println( "OK" ); // 显示调用结果

	}
	
	//The following method isn't working yet on 2015.04.22
	private void testFavoriteWithAnnoation(){
		Resource r=new Resource();
		//String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "AnnotationConsumer.xml";
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
//    	fc=new FavoriteConsumer();
//    	fc.consume(r);
//        fService.doFavorite(r);
 
        System.out.println( "OK" ); // 显示调用结果

	}
	
	private void testUpload(){
		Resource r=new Resource();
//		r.setId(101);
		r.setResourceId("abcduuidabcduuid-inside-20150511-4");
		r.setTitle("中文标题也没问题了-4");
//		r.setTimestampId(1);
		Tag[] tArray=new Tag[1];
		tArray[0]=new Tag();
//		tArray[0].setId(101);
		tArray[0].setNameCn("中文测试");  //2015.04.16 插入数据库中文会乱码
		//tArray[0].setNameCn("SpringTest");
		
		String config = dubboConsumer.class.getPackage().getName().replace('.', '/') + "/" + "consumer.xml";
		
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
        context.start();
 
        UploadService demoService = (UploadService)context.getBean("doUpload"); // 获取远程服务代理
        DubboResult drResult= demoService.doUpload(r, tArray);; // 执行远程方法
        assertEquals(0,drResult.getCode());
 
        System.out.println( "OK" ); // 显示调用结果

	}

}
